﻿namespace Smart.Utils.Net
{
    using Microsoft.Win32;
    using System;
    using System.IO;
    using System.Text;

    public class Attachment
    {
        private string content_type;
        private string encoding;
        private string file_name;
        private string file_path;
        private long size;

        public Attachment(string file_path)
        {
            this.file_path = file_path;
            this.encoding = "base64";
            if (this.file_path.Length > 0)
            {
                FileInfo info = new FileInfo(this.file_path);
                if (info.Exists)
                {
                    this.file_name = info.Name;
                    this.size = info.Length;
                    this.content_type = this.GetContentType(info.Extension);
                }
            }
        }

        private string GetContentType(string file_extension)
        {
            try
            {
                string str = Registry.ClassesRoot.OpenSubKey(file_extension).GetValue("Content Type").ToString();
                if ((str != "") && (str != null))
                {
                    return str;
                }
                return "application/octet-stream";
            }
            catch (Exception)
            {
                return "application/octet-stream";
            }
        }

        public string GetMimeMessage()
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("Content-Type: " + this.content_type + "; ");
            builder.Append("name=\"" + this.file_name + "\"\r\n");
            builder.Append("Content-Transfer-Encoding: " + this.encoding + "\r\n");
            builder.Append("Content-Disposition: attachment; ");
            builder.Append("filename=\"" + this.file_name + "\"\r\n\r\n");
            builder.Append(MailEncoder.GetBase64String(this.file_path));
            return builder.ToString();
        }

        public string ContentType
        {
            get
            {
                return this.content_type;
            }
        }

        public string EncodingType
        {
            get
            {
                return this.encoding;
            }
        }

        public string FileName
        {
            get
            {
                return this.file_name;
            }
        }

        public long FileSize
        {
            get
            {
                return this.size;
            }
        }
    }
}

